package com.example.controller;

import com.example.common.BaseContext;
import com.example.common.Re;
import com.example.entity.AddressBook;
import com.example.service.IAddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 地址管理
 * </p>
 *
 * @author czx
 * @since 2022-04-28
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Resource
    private IAddressBookService addressBookService;

    /**
     * 查询用户地址
     *
     * @return 结果
     */
    @GetMapping("/list")
    public Re<List<AddressBook>> list() {
        Long userId = BaseContext.getCurrentId();
        log.info("查询用户地址： userId = {}", userId);
        List<AddressBook> addressBookList = addressBookService.listByUserId(userId);
        if (addressBookList != null) {
            return Re.success(addressBookList);
        }
        return Re.error("获取地址列表失败");
    }

    /**
     * 保存用户地址信息
     *
     * @param addressBook 地址信息
     * @return 结果
     */
    @PostMapping
    public Re<String> save(@RequestBody AddressBook addressBook) {
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        boolean save = addressBookService.save(addressBook);
        if (save) {
            return Re.success("地址保存成功");
        }
        return Re.error("保存地址失败");
    }

    /**
     * 更新默认地址
     *
     * @param addressBook 要更新的地址
     * @return 结果
     */
    @PutMapping("/default")
    public Re<String> updateIsDefault(@RequestBody AddressBook addressBook) {
        log.info("默认地址： id = {}", addressBook.getId());
        boolean updateIsDefault = addressBookService.updateByIsDefault(addressBook);
        if (updateIsDefault) {
            return Re.success("默认地址修改成功");
        }
        return Re.error("默认地址修改失败");
    }

    /**
     * 回显编辑地址
     *
     * @param id 地址唯一标识
     * @return 地址结果
     */
    @GetMapping("/{id}")
    public Re<AddressBook> getById(@PathVariable Long id) {
        log.info("回显地址信息：id={}", id);
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null) {
            return Re.success(addressBook);
        }
        return Re.error("地址信息回显失败");
    }

    /**
     * 修改地址信息
     *
     * @param addressBook 地址信息
     * @return 结果
     */
    @PutMapping
    public Re<String> update(@RequestBody AddressBook addressBook) {
        boolean update = addressBookService.updateById(addressBook);
        if (update) {
            return Re.success("地址信息修改成功");
        }

        return Re.error("地址信息修改失败");
    }

    /**
     * 删除地址信息
     *
     * @param ids 地址id
     * @return 结果
     */
    @DeleteMapping
    public Re<String> delete(@RequestParam List<Long> ids) {
        log.info("删除地址信息：ids={}",ids);
        boolean b = addressBookService.removeBatchByIdsAndIsDeleted(ids);
        if (b) {
            return Re.success("地址删除成功");
        }
        return Re.error("默认地址不能删除");
    }

    @GetMapping("/default")
    public Re<AddressBook> getByIsDefault(){
        Long currentId = BaseContext.getCurrentId();
        log.info("默认地址获取：{}",currentId);
        AddressBook addressBook = addressBookService.getByIsDefault(currentId);
        if(addressBook!=null){
            return Re.success(addressBook);
        }
        return Re.error("默认地址获取失败");
    }
}
